package com.duowei.diandan.app;

import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.android.print.sdk.PrinterConstants;
import com.duowei.diandan.activity.WelcomeActivity;
import com.duowei.diandan.constant.PrintMode;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.event.CountdownEvent;
import com.duowei.diandan.print2.IPrinterOperation;
import com.duowei.diandan.print2.UsbOperation;
import com.duowei.diandan.print3.Print3;
import com.duowei.diandan.service.DataSyncService;
import com.duowei.diandan.service.TouchService;

import org.greenrobot.eventbus.EventBus;

import java.util.Timer;
import java.util.TimerTask;

public class InitManager {
    private static final String TAG = "InitManager";
    private static InitManager INSTANCE;

    private App mApp;
    private Intent mTouchServiceIntent;

    private boolean mServiceRunning = false;
    private boolean mCanDownTime = true;

    private int mMaxCountdownSecond;
    private int mCountdownSecond;
    private Timer mTimer;

    private IPrinterOperation mOperation;

    private Handler mHandler = new PrintHandler();

    private InitManager() {
        mApp = App.getApp();
        mTouchServiceIntent = new Intent(mApp, TouchService.class);
    }

    public static InitManager getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new InitManager();
        }

        return INSTANCE;
    }

    public void start() {
        Log.d(TAG, "start: ");

        if (!mServiceRunning) {
            startService();
        }

        if (PrintMode.INSIDE_MODE == Settings.PRINTER_MODE) {
            if (mOperation == null) {
                mOperation = new UsbOperation(mApp, mHandler);
                UsbManager um = (UsbManager) mApp.getSystemService(Context.USB_SERVICE);
                mOperation.usbAutoConn(um);
            } else {
                UsbManager um = (UsbManager) mApp.getSystemService(Context.USB_SERVICE);
                mOperation.usbAutoConn(um);
            }

        } else if (PrintMode.INSIDE_MODE_GUANG_ZHOU == Settings.PRINTER_MODE) {
            if (!Print3.getInstance().isConnected()) {
                Print3.getInstance().connect(null);
            }
        }

        //
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (Settings.COUNTDOWN_ENABLED) {
            mTimer = new Timer();
            mTimer.schedule(new Task(), 1000, 1000);
        }
    }

    private void startService() {
        Log.d(TAG, "startService: ");

        mServiceRunning = true;

        mApp.startService(mTouchServiceIntent);
    }

    public void stopService() {
        Log.d(TAG, "stopService: ");

        mServiceRunning = false;

        mApp.stopService(mTouchServiceIntent);
        mApp.stopService(new Intent(mApp, DataSyncService.class));

        if (mTimer != null) {
            mTimer.cancel();
        }
    }

    public void setRestTouchTime() {
        Log.d(TAG, "setRestTouchTime: ");

        mCountdownSecond = mMaxCountdownSecond + 1;
    }

    public void setMaxCountdownSecond(int maxCountdownSecond) {
        mMaxCountdownSecond = maxCountdownSecond;
        setRestTouchTime();
    }

    public void setCanDownTime(boolean canDownTime) {
        Log.d(TAG, "setCanDownTime: " + canDownTime);

        mCanDownTime = canDownTime;
        if (mCanDownTime) {
            mCountdownSecond = Settings.COUNTDOWN_SECOND;
        }
    }

    public int getCountdownSecond() {
        return mCountdownSecond;
    }

    public IPrinterOperation getOperation() {
        return mOperation;
    }

    private class Task extends TimerTask {

        @Override
        public void run() {
            if (mApp.getLifecycleCallbacks().isApplicationInForeground()) {
                if (mCanDownTime) {
                    mCountdownSecond--;
                    EventBus.getDefault().post(new CountdownEvent(mCountdownSecond));

                    if (mCountdownSecond <= 0) {
                        Intent intent = new Intent(mApp, WelcomeActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        mApp.startActivity(intent);
                    }
                }

            }

            // mApp.getLifecycleCallbacks().log();
            Log.e(TAG, " TouchTime: " + mCountdownSecond);
        }
    }

    private static class PrintHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PrinterConstants.Connect.SUCCESS:
                    Log.d(TAG, "handleMessage: 内置打印机连接失败");
//                    Toast.makeText(App.getApp(), "内置打印机连接成功", Toast.LENGTH_SHORT).show();
                    break;
                case PrinterConstants.Connect.FAILED:
                    Log.d(TAG, "handleMessage: 内置打印机连接失败");
                    Toast.makeText(App.getApp(), "内置打印机连接失败", Toast.LENGTH_SHORT).show();
                    break;
                case PrinterConstants.Connect.CLOSED:
                    Log.d(TAG, "handleMessage: 内置打印机连接关闭");
                    Toast.makeText(App.getApp(), "内置打印机连接关闭", Toast.LENGTH_SHORT).show();
                    break;
                case PrinterConstants.Connect.NODEVICE:
                    Log.d(TAG, "handleMessage: 内置打印机未发现");
                    Toast.makeText(App.getApp(), "内置打印机未发现", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}
